<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\Agent;
use App\Models\Complain;
use Carbon\Carbon;
use App\Models\BadOpenid;

class CheckBaseParams
{
    /**
     * 检测每次访问的 header 参数，refer
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if($request->has('code') && $request->code){
            $code = $request->code;
            if($request->has('base_code')){//支付授权登录页
                 $code = $request->base_code;
            }
            if(($res=$this->checkFromWx())===true){
                if(($res = $this->checkParam($code))===true){
                    return $next($request);
                }
                return errorGo($res);
            }
            return errorGo($res);
        }
        return errorGo('nocode');
        
    }

    /**
     * 检查code
     *
     * @param [type] $request
     * @return void
     */
    protected function checkParam($code){
        if(strpos($_SERVER['HTTP_USER_AGENT'],'Windows NT') !== false){ 
            return errorGo('from_pc');
        }
        $code_data = decodeCode($code);
        //解析后的code是数组
        if(is_array($code_data) && count($code_data)>3){
            $from_time = $code_data[3];
            $openid = $code_data[2];
            //code有效期半个小时
            if(strtotime(Carbon::now()->subHours(20))>$from_time){
                return 'error_time';
            }
            //是否封禁的openid
            // $bad_openid = cache()->remember("bad_openid:{$openid }",Carbon::now()->addDays(1),function()use($openid ){
            //     $exists = BadOpenid::where(['openid'=>$openid])->exists();
            //     return $exists;
            // } );
            // if($bad_openid){
            //     return 'bad_user';
            // }
            //是否是同一个设备访问的
            $link_ua = cache()->rememberForever("openid_ua:{$code_data[2]}",function(){
                return  md5(filterUa($_SERVER['HTTP_USER_AGENT']));
            } );
            if($link_ua !=  md5(filterUa($_SERVER['HTTP_USER_AGENT']))){
                return 'error_user';
            }
            //检查代理
            if(config('visit.check_agent')){
                $agent_id = $code_data[1];
                if(Agent::enable()->find($agent_id)){
                    return true;
                }
                else{
                    return 'error_agent';
                }
            }
            return true;
        }
        else{
            return 'error_code';
        }
    }

    /**
     * 检查是否微信客户端
     *
     * @return void
     */
    protected function checkFromWx(){
        $ua = $_SERVER['HTTP_USER_AGENT'];
        //是否只能微信访问
        if(config('visit.wechat_visit_only')){
            if(strpos($ua, 'MicroMessenger') == false){ 
                //非微信内置浏览器
                return 'nowechat';
            }
        }
        //检查是否开发者工具
        if(config('visit.no_wechatdevtools')){
            if(strpos($ua,'wechatdevtools')){
                return 'no_dev';
            }
        }
        //"来自微信浏览器访问"; 
        return true;
    }
}
